using System;
using System.Collections.Generic;
using System.IO;

namespace DxfParser
{
    public class DxfEntity
    {
        public string EntityType { get; set; }
        public Dictionary<int, string> Properties { get; set; }

        public DxfEntity()
        {
            Properties = new Dictionary<int, string>();
        }
    }

    public class DxfParser
    {
        private List<DxfEntity> entities;

        public DxfParser()
        {
            entities = new List<DxfEntity>();
        }

        public List<DxfEntity> Parse(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("DXF file not found", filePath);
            }

            string[] lines = File.ReadAllLines(filePath);
            DxfEntity currentEntity = null;

            for (int i = 0; i < lines.Length; i += 2)
            {
                if (i + 1 >= lines.Length) break;

                int groupCode;
                if (!int.TryParse(lines[i], out groupCode))
                    continue;

                string value = lines[i + 1].Trim();

                // 开始一个新实体
                if (groupCode == 0)
                {
                    if (currentEntity != null)
                    {
                        entities.Add(currentEntity);
                    }
                    currentEntity = new DxfEntity
                    {
                        EntityType = value
                    };
                }
                else if (currentEntity != null)
                {
                    currentEntity.Properties[groupCode] = value;
                }
            }

            // 添加最后一个实体
            if (currentEntity != null)
            {
                entities.Add(currentEntity);
            }

            return entities;
        }

        public void PrintEntities()
        {
            foreach (var entity in entities)
            {
                Console.WriteLine($"Entity Type: {entity.EntityType}");
                foreach (var prop in entity.Properties)
                {
                    Console.WriteLine($"Group Code {prop.Key}: {prop.Value}");
                }
                Console.WriteLine();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter DXF file path:");
            string filePath = Console.ReadLine();

            try
            {
                var parser = new DxfParser();
                var entities = parser.Parse(filePath);
                parser.PrintEntities();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}
